package com.random.fox.whatspay.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;

import com.gc.materialdesign.views.ButtonFlat;
import com.random.fox.whatspay.R;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by 渠 on 2014/11/7.
 */
public class TimePicker extends Dialog{
    private TimeCircle timeCircle;
    private ButtonFlat complete;
    private ButtonFlat cancel;

    private TimePickListener mListener;

    public TimePicker(Context context,Date date) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.time_picker_layout);


        timeCircle = (TimeCircle) findViewById(R.id.time_circle);
        complete = (ButtonFlat) findViewById(R.id.button_ok);
        cancel = (ButtonFlat) findViewById(R.id.button_cancel);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        timeCircle.setCurrentHour(calendar.get(Calendar.HOUR));
        timeCircle.setCurrentMinute(calendar.get(Calendar.MINUTE));
        setListener();
    }

    private void setListener(){
        complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener !=null){
                    mListener.onComplete(getDate());
                }
                dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener !=null){
                    mListener.onCancel();
                }
                dismiss();
            }
        });
    }

    public void setTimePickListener(TimePickListener listener){
        mListener = listener;
    }

    public Date getDate(){
        return timeCircle.getDate();
    }

    public static interface TimePickListener{
        void onComplete(Date date);
        void onCancel();
    }

}
